<?php
/**
 * Created by PhpStorm.
 * User: zhiduopin-PC
 * Date: 2018/10/25
 * Time: 16:33
 */
/**
 * Notes: 获取用户的昵称
 * User: 王俊成
 * Date: 2018/10/25
 * Time: 16:38
 * @param $user_id
 * @return mixed|null|string|string[]
 */
use think\cache\driver\Redis;

function get_user_name($user_id){
    $userModel = new \app\admin\model\User();
    //$nickname = $userModel->where('id',$user_id)->value('nickname');
    $nickname = $userModel->where('user_id',$user_id)->value('nickname');
    if($nickname)
        $nickname = preg_replace_callback('/@E(.{6}==)/', function($r) {return base64_decode($r[1]);},$nickname);
    else
        $nickname = '';
    return $nickname;
}

/**
 * Notes: 获取用户uid
 * User: 王俊成
 * Date: 2018/10/29
 * Time: 17:26
 * @param $user_id
 * @return mixed|string
 */
function get_uid($user_id){
    $userModel = new \app\admin\model\User();
    $uid = $userModel->where('id',$user_id)->value('u_id');
    if($uid)
        return $uid;
    else
        return '';
}

/**
 * Notes: 获取礼物名
 * User: 王俊成
 * Date: 2018/10/26
 * Time: 15:34
 * @param $gift_id
 * @return mixed|string
 */
function get_gift_name($gift_id){
    $giftUrlModel = new \app\admin\model\GiftUrl();
    $giftName = $giftUrlModel->where('id',$gift_id)->value('gift_name');
    if($giftName)
        return $giftName;
    else
        return '';
}

/**
 * Notes: 获取礼物图片
 * User: 王俊成
 * Date: 2018/10/26
 * Time: 15:35
 * @param $gift_id
 * @return mixed|string
 */
function get_gift_pic($gift_id){
    $giftUrlModel = new \app\admin\model\GiftUrl();
    $giftPic = $giftUrlModel->where('id',$gift_id)->value('gift_url');
    if($giftPic)
        return $giftPic;
    else
        return config('site_url').'/static/admin/images/nickPic.png';
}

/**
 * Notes: 获取房间名
 * User: 王俊成
 * Date: 2018/11/1
 * Time: 14:57
 * @param $room_id
 * @return mixed|string
 */
function get_room_name($room_id){
    $roomModel = new \app\admin\model\Room();
    $roomName = $roomModel->where('id',$room_id)->value('r_name');
    if($roomName)
        return $roomName;
    else
        return '';
}

/**
 * Notes: 获取家族名
 * User: 王俊成
 * Date: 2018/11/2
 * Time: 15:00
 * @param $family_id
 * @return mixed|string
 */
function get_family_name($family_id){
    $familyModel = new \app\admin\model\Family();
    $familyName = $familyModel->where('id',$family_id)->value('f_name');
    if($familyName)
        return $familyName;
    else
        return '';
}

/**
 * 帅帅 吴
 * 阿里云OSS图片上传
 * @$ossUploadPath,设置要上传到oss的某个目录
 * @return array
 */
function oosUpload($file,$dir='ss-gift',$endpoint='oss-cn-beijing.aliyuncs.com')
{
    if ($file) {
        // $fileAll = $_FILES;
        // foreach($fileAll as $k => $file){
        if ($file['error'] === 0) {
            $name = $file['name'];
            $format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
            /*判断图片格式*/
            $allow_type = ['.jpg', '.jpeg', '.gif', '.bmp','.webp', '.png','.svga'];
            if (!in_array($format, $allow_type)) {
                return ['code' => 100, 'msg' => '文件格式不在允许范围内哦'];
            }
            // 尝试执行
            try {
                //  $config = config('app.oos');
                // var_dump($config);
                //实例化对象 将配置传入
                $ossClient = new \OSS\OssClient('LTAI4FkTgFd92TZiA5enAaf6', 'EFFF7ukoCDOjtCFHdHaf2MmIEKIavR', $endpoint);
                //这里是有sha1加密 生成文件名 之后连接上后缀
                // $fileName = $ossUploadPath . '/' . date("Ymd") . '/' . sha1(date('YmdHis', time()) . uniqid()) . $format;
                $fileName = sha1(date('YmdHis', time()) . uniqid()) . $format;
                //执行阿里云上传
                $result = $ossClient->uploadFile($dir, $fileName, $file['tmp_name']);
                /*组合返回数据*/
                $arr = [
                    'oss_url' => $result['info']['url'],  //上传资源地址
                    'relative_path' => $fileName     //数据库保存名称(相对路径)
                ];
            } catch (\OSS\Core\OssException $e) {
                return ['code' => -100, 'msg' => $e->getMessage()];
            }
            //将结果返回
            return $arr['oss_url'];
        }
        return null;
    }else
        return  null;

}


//创建机器人的u_id
function create_u_id($u_id = false)
{
    $userModel = new \app\admin\model\User();
    $niceIdModel = new \app\admin\model\NiceId();
    if(!$u_id){
        $u_id = $userModel->max('u_number');
        $u_id = $u_id + 1;
    }
    $re = $niceIdModel->where('nice_num', 'eq', $u_id)->find();
    $res = $userModel->where('u_number', 'eq', $u_id)->find();
    if ($re || $res) {
        return create_u_id($u_id+1);
    }
    return $u_id;


}

/**
 * 生成唯一token
 * @return string
 */
function settoken()
{
    $str = md5(uniqid(md5(microtime(true)), true));  //生成一个不会重复的字符串
    $str = sha1($str);  //加密
    return $str;
}

//网易云im修改个人信息
function updateServerInfo($accid, $name = '', $icon = '', $sign = '', $email = '', $birth = '', $mobile = '', $gender = '0', $ex = '')
{
    $p = new think\wyyxapi\WyYxAPI();;
    $result = $p->updateUinfo($accid, $name, $icon, $sign, $email, $birth, $mobile, $gender, $ex);
    return $result;
}

//网易云im用户管理
function ServerUser($info, $type)
{
    $p = new think\wyyxapi\WyYxAPI();
    if ($type == "create") {
        $re = $p->createUserId($info);
    } elseif ($type == "update") {
        $re = $p->updateUserId($info);
    } elseif ($type == "updateToken") {
        $re = $p->updateUserToken($info);
    }
    return $re;
}

//创建聊天室
function create_chatroom($creator, $name = '', $announcement = '', $broadcasturl = '', $ext = '')
{
    $p = new think\wyyxapi\WyYxAPI();
    $result = $p->createChatroom($creator, $name, $announcement, $broadcasturl, $ext);
    return $result;
}

//个人头衔
/**
 * @param $user_id
 * @param $type
 * @return array|string
 * 个人头衔 ['user_id'=>'img']
 */
function personalTitle($user_id,$type=3){

    $time = time();
    $begin = '2017-12-31 00:30:00';
    $end = '2019-12-31 23:59:59';

    if($time < strtotime($begin) || $time > strtotime($end)){ //在有效期展示头衔
        return '';
    }

    $redis = new \Redis();
    $redis->connect('127.0.0.1', 6379);
    $redis->auth('susu123');
    //财富和魅力头衔
    $title = $redis->get('personalTitle');
    if(!$title){
        return '';
    }
    $res = [];
    $title = json_decode($title,true);
    switch ($type){
        case 1:
            if(isset($title['wealth'][$user_id]) && !empty($title['wealth'][$user_id])){
                array_push($res,$title['wealth'][$user_id]);
            }
            break;
        case 2:
            if(isset($title['charm'][$user_id]) && !empty($title['charm'][$user_id])){
                array_push($res,$title['charm'][$user_id]);
            }
            break;
        case 3:
            if(isset($title['wealth'][$user_id]) && !empty($title['wealth'][$user_id])){
                array_push($res,$title['wealth'][$user_id]);
            }

            if(isset($title['charm'][$user_id]) && !empty($title['charm'][$user_id])){
                array_push($res,$title['charm'][$user_id]);
            }
            break;
    }
    if(!empty($res)){
        return $res;
    }
    return '';
}

//获取打款方式
function get_cheques_type($cheques){

    $arr = [1=>'支付宝','微信','银行卡'];
    return $arr[$cheques] ? $arr[$cheques] : '未知方式';
}